package com.zhuoan.im.webapp.controller;

import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.zhuoan.im.webapp.biz.UserBiz;
import com.zhuoan.im.webapp.common.PaginationEnum;
import com.zhuoan.im.webapp.common.ResCodeEnum;
import com.zhuoan.im.webapp.common.exception.BizException;
import com.zhuoan.im.webapp.dao.model.condition.UserCondition;
import com.zhuoan.im.webapp.dao.model.vo.UserVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * DemoController
 *
 * @author weixiang.wu
 * @date 2018 -08-22 21:05
 */
@RestController
@RequestMapping("/")
public class DemoController extends BaseController{

    private final static Logger logger = LoggerFactory.getLogger(DemoController.class);


    @Resource
    private UserBiz userBiz;

	@RequestMapping("ping")
	public String test() {
		return "SUCCESS";
	}

    @RequestMapping(value = "queryByCondition", method = RequestMethod.GET)
    @ResponseBody
    public String queryByCondition(UserCondition userCondition, String draw) {
        Map<String, Object> resultMap = new HashMap<>();
        userCondition.setPageLimit(getPageLimit());
        logger.info("查数据开始");
        PageList<UserVO> userVOS = userBiz.queryAllUsersByCondition(userCondition);
        /**
         * 抛出异常测试.
         */
//        if (Boolean.TRUE) {
//            throw new BizException(ResCodeEnum.OTHER.getResMessage(), ResCodeEnum.OTHER.getResCode());
//        }
        resultMap.put(PaginationEnum.DATA.getConstant(), userVOS);
        resultMap.put(PaginationEnum.DRAW.getConstant(), draw);
        resultMap.put(PaginationEnum.RECORDS_TOTAL.getConstant(), userVOS.getPaginator().getTotalCount());
        resultMap.put(PaginationEnum.RECORDS_FILTERED.getConstant(), userVOS.getPaginator().getTotalCount());
        resultMap.put(ResCodeEnum.RES_CODE.getResCode(), ResCodeEnum.SUCCESS.getResCode());
        resultMap.put(ResCodeEnum.RES_MSG.getResCode(), ResCodeEnum.SUCCESS.getResMessage());
        return objectToJson(resultMap);
    }
}
